<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryRef"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="区域名称" prop="qymc">
        <el-input
          v-model="queryParams.qymc"
          placeholder="请输入区域名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['yqhs:qy:add']"
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Sort" @click="toggleExpandAll"
          >展开/折叠</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="qyList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- <el-table-column label="上级ID" prop="parentId" /> -->
      <el-table-column label="区域名称" align="center" prop="qymc" />
      <el-table-column label="备注" align="center" prop="rekark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-hasPermi="['yqhs:qy:edit']"
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['yqhs:qy:add']"
            link
            type="primary"
            icon="Plus"
            @click="handleAdd(scope.row)"
            >新增</el-button
          >
          <el-button
            v-hasPermi="['yqhs:qy:remove']"
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改区域信息对话框 -->
    <el-dialog v-model="open" :title="title" width="500px" append-to-body>
      <el-form ref="qyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级区域" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="qyOptions"
            :props="{ value: 'id', label: 'qymc', children: 'children' }"
            value-key="id"
            placeholder="请选择上级区域"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="区域名称" prop="qymc">
          <el-input v-model="form.qymc" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="备注" prop="rekark">
          <el-input
            v-model="form.rekark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Qy">
import { listQy, getQy, delQy, addQy, updateQy } from "@/api/yqhs/qy";

const { proxy } = getCurrentInstance();

const qyList = ref([]);
const qyOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    parentId: null,
    qymc: null,
    rekark: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询区域信息列表 */
function getList() {
  loading.value = true;
  listQy(queryParams.value).then((response) => {
    qyList.value = proxy.handleTree(response.data, "id", "parentId");
    loading.value = false;
  });
}

/** 查询区域信息下拉树结构 */
function getTreeselect() {
  listQy().then((response) => {
    qyOptions.value = [];
    const data = { id: 0, qymc: "顶级节点", children: [] };
    data.children = proxy.handleTree(response.data, "id", "parentId");
    qyOptions.value.push(data);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: null,
    qymc: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    rekark: null,
  };
  proxy.resetForm("qyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加区域信息";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.parentId;
  }
  getQy(row.id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改区域信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["qyRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateQy(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addQy(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除区域信息编号为"' + row.id + '"的数据项？')
    .then(function () {
      return delQy(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
